//
//  HPCustomerSmsViewController.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/30/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPCustomerSmsViewController.h"
#import <UITextView+Placeholder/UITextView+Placeholder.h>
#import "KSViewPagerView.h"

#import "HPSmsCell.h"
#import "HPHttpSmsQueryReqeust.h"

@interface HPCustomerSmsViewController()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *systemSmsArray;
@property (nonatomic, strong) NSArray *favoriteSmsArray;

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) KSViewPagerView *pagerView;

@property (nonatomic, strong) HPHttpSmsQueryReqeust *systemRequest;
@property (nonatomic, strong) HPHttpSmsQueryReqeust *favoriteRequest;

@end

@implementation HPCustomerSmsViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    [self addBackButton:nil];
    [self setBaseNavigationBarTitle:@"短信编辑"];
    
    [self.baseRightButton setTitle:@"发送" forState:UIControlStateNormal];
    
    [self setupUI];
    
    WS(ws);
    self.systemRequest = [[HPHttpSmsQueryReqeust alloc] init];
    self.systemRequest.flag = SMSQueryFlag_System;
    self.systemRequest.type = SMSQueryType_NearMemorial;
    self.systemRequest.responseBlock = ^(HPHttpSmsQueryResponse *response, HttpError *error){
        if (error) {
            
        }else{
            ws.systemSmsArray = response.smsArray;
            [ws.tableView reloadData];
        }
    };
    [self.systemRequest send];
    
    self.favoriteRequest = [[HPHttpSmsQueryReqeust alloc] init];
    self.favoriteRequest.flag = SMSQueryFlag_Self;
    self.favoriteRequest.type = SMSQueryType_NearMemorial;
    self.favoriteRequest.responseBlock = ^(HPHttpSmsQueryResponse *response, HttpError *error){
        if (error) {
            
        }else{
            ws.favoriteSmsArray = response.smsArray;
            [ws.tableView reloadData];
        }
    };
    [self.favoriteRequest send];
}


#pragma mark - Private
- (void)setupUI{
    WS(ws);
    
    UIView *panel = [UIView new];
    [self.baseContentView addSubview:panel];
    [panel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.baseContentView).offset(-20);
        make.top.equalTo(self.baseContentView).offset(10);
        make.centerX.equalTo(self.baseContentView);
        make.height.equalTo(@150);
    }];
    panel.layer.borderColor = [UIColor colorWithRGB:0xe0e0e0].CGColor;
    panel.layer.borderWidth = 1;
    
    UITextView *textView = [UITextView new];
    [panel addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(panel).offset(-10);
        make.center.equalTo(panel);
    }];
    textView.font = [UIConstants getFont:17];
    textView.placeholder = @"短信内容";
    self.textView = textView;
    
    UILabel *remainLabel = [UILabel new];
    [self.baseContentView addSubview:remainLabel];
    [remainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseContentView);
        make.top.equalTo(panel.mas_bottom);
        make.height.equalTo(@40);
    }];
    remainLabel.text = @"剩余0字";
    remainLabel.textColor = [UIColor redColor];
    
    KSViewPagerView *pager = [[KSViewPagerView alloc] initWithArray:@[@"官方推荐",@"我的收藏"]];
    [self.baseContentView addSubview:pager];
    pager.backgroundColor = [UIColor whiteColor];
    [pager mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.equalTo(self.baseContentView);
        make.height.equalTo(@40);
        make.top.equalTo(remainLabel.mas_bottom).offset(10);
    }];
    
    [pager setActiveColor:[UIColor colorWithRGB:0x4fbcff]];
    [pager setInactiveColor:[UIColor colorWithRGB:0xafafaf]];
    [pager setTitleFont:[UIConstants getFont:15]];
    pager.click = ^(KSViewPagerView*v, NSNumber *index){
        [ws.tableView reloadData];
    };
    
    [pager setCursorPosition:0];
    self.pagerView = pager;
    
    UITableView *tableView = [UITableView new];
    [self.baseContentView addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pager.mas_bottom);
        make.width.centerX.bottom.equalTo(self.baseContentView);
    }];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [UIView new];
    self.tableView = tableView;
}

- (NSArray *)getDatas{
    if ([self.pagerView getSelectedSegment]==0) {
        return self.systemSmsArray;
    }else{
        return self.favoriteSmsArray;
    }
}

#pragma UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self getDatas].count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.pagerView getSelectedSegment]==0) {
        return [self tableView:tableView cellForSystemRowAtIndexPath:indexPath];
    }else{
        return [self tableView:tableView cellForMyRowAtIndexPath:indexPath];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForSystemRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"CustomerSystemSmsCell";
    HPSmsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HPSmsCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        
        NSMutableArray *rightUtilityButtons = [NSMutableArray new];
        
        [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRGB:0x058cc0]
                                                     icon:[UIImage imageNamed:@"Button_AddTask"]];
        cell.rightUtilityButtons = rightUtilityButtons;
    }
    NSArray *a = [self getDatas];
    SmsObject *obj = a[indexPath.row];
    [cell setup:obj.content];
    
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForMyRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"CustomerMySmsCell";
    HPSmsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HPSmsCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        
    }
    
    NSArray *a = [self getDatas];
    SmsObject *obj = a[indexPath.row];
    [cell setup:obj.content];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray *a = [self getDatas];
    SmsObject *obj = a[indexPath.row];
    return [HPSmsCell getCellHeight:obj.content];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *a = [self getDatas];
    SmsObject *obj = a[indexPath.row];
    
    self.textView.text = [AppHelper formatSms:obj.content name:self.vip.vipName gender:self.vip.vipGender];
}
@end
